'use strict';
const mongoose = require('mongoose');

module.exports={
    //
    list:async function(options={group:'',type:'',minTimeTick:0,maxTimeTick:0}){
        let CensusModel=mongoose.models['Census'];
        let now=new Date().getTime();
        let query={
            timeTick:{$gte:now-86400000,$lt:now}
        };
        if(!!options.group){
            query.group=options.group;
        }
        if(!!options.type){
            if(options.type != '无条件'){
                query.type=options.type;
            }
        }
        if(!!options.minTimeTick){
            query.timeTick.$gte=parseInt(options.minTimeTick);
        }
        if(!!options.maxTimeTick){
            query.timeTick.$lt=parseInt(options.maxTimeTick);
        }
        let census=await CensusModel.find(query);
        //channels.sort((a,b)=>{return b.sort-a.sort;});
        return census;
    },
    eventsType: async function(options){
        let keys = {
            showVideo: 'showVideo',         //点击了观看视频
            VideoTrue: 'VideoTrue',         //看视频成功
            VideoFalse: 'VideoFalse',       //看视频失败
            receiveRewards: 'receiveRewards',               //普通领取
            receiveDoubleRewards: 'receiveDoubleRewards',   //双倍领取(分享)
            receiveFiveRewards: 'receiveFiveRewards',       //五倍领取(看视频)
            TaskReportRewards: 'TaskReportRewards',             //单倍领取
            TaskReportDoubleRewards: 'TaskReportDoubleRewards'  //双倍领取(看视频)
        };
        return keys;
    },

    async hotEvent(options){
        console.log('hotevent start');
        let CensusModel=mongoose.models['Census'];
        let condition = {timeTick:{$gte:parseInt(options.beforeTime),$lte:parseInt(options.afterTime)}};
        if(!!options.scene){
            condition.type = 'hot_'+options.scene
        }
        let datas = await CensusModel.find(condition);
        let res = [];
        for(let i=0; i<datas.length; i++){
            let data = datas[i]
            let _p = data.group.indexOf('_',1)
            res.push({
                timeTick:data.timeTick,
                x:data.group.substring(0,_p),
                y : data.group.substring(_p+1),
                scene : options.scene
            })
        }
        // resp.json({code:200,datas:res})
        return res;
    }


};

// module.exports = schema;